<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          size="mini"
          label-width="98px"
        >
          <el-form-item label="业务类型" prop="rdCode">
            <el-select
              v-model.trim="queryParams.rdCode"
              placeholder="请选择业务类型"
              clearable
            >
              <el-option
                v-for="dict in dict.type.sgs_rd_code"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="进出口标志" prop="iEFlag">
            <el-select
              v-model.trim="queryParams.iEFlag"
              placeholder="请选择进出口标志"
              clearable
            >
              <el-option
                v-for="dict in dict.type.sys_i_e_flag"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="做单所属海关" prop="customsCode">
            <el-select
              v-model.trim="queryParams.customsCode"
              placeholder="请选择做单所属海关"
              clearable
            >
              <el-option
                v-for="dict in customsList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="做单场所代码" prop="fieldCode">
            <el-select
              v-model.trim="queryParams.fieldCode"
              placeholder="请选择做单场所代码"
              clearable
            >
              <el-option
                v-for="dict in fieldOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="过卡车辆类型" prop="bayonetRdCode">
            <el-select
              v-model.trim="queryParams.bayonetRdCode"
              placeholder="请选择过卡车辆类型"
              clearable
            >
              <el-option
                v-for="dict in dict.type.sgs_bayonet_rd_code"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申报状态" prop="declStatus">
            <el-select
              v-model.trim="queryParams.declStatus"
              placeholder="请选择申报状态"
              clearable
            >
              <el-option
                v-for="dict in dict.type.sgs_decl_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="报关企业代码" prop="entCode">
            <el-input
              v-model.trim="queryParams.entCode"
              placeholder="请输入报关企业代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="daterangeCreateTime"
              class="reset-clear"
              type="datetimerange"
              :clearable="false"
              unlink-panels
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
            />
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddEdit('add')"
            v-hasPermi="['bt:sgsBulkCargoHead:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['bt:sgsBulkCargoHead:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
             :disabled="multiple"
            size="mini"
            @click="handleDecl"
            v-hasPermi="['bt:sgsBulkCargoHead:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleAddEdit('declModify')"
            v-hasPermi="['bt:sgsBulkCargoHead:decl']"
          >变更申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            :disabled="single"
            @click="handleGenTakecargoBill"
            v-hasPermi="['bt:sgsBulkCargoHead:genbill']"
          >生成作业单
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleCancelDecl"
            v-hasPermi="['bt:bt:sgsBulkCargoHead:decl']"
          >撤销申报
          </el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['bt:sgsBulkCargoHead:export']"
          >导出</el-button>
        </el-col>
        -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <template #bindKeyType="scope">
          <dict-tag
            :options="dict.type.sgs_bind_key_type"
            :value="scope.row.bindKeyType"
          ></dict-tag>
        </template>

        <template #rdCode="scope">
          <dict-tag
            :options="dict.type.sgs_bill_rd_code"
            :value="scope.row.rdCode"
          ></dict-tag>
        </template>

        <template #iEFlag="scope">
          <dict-tag
            :options="dict.type.sys_i_e_flag"
            :value="scope.row.iEFlag"
          ></dict-tag>
        </template>

        <template #bayonetRdCode="scope">
          <dict-tag
            :options="dict.type.sgs_bayonet_rd_code"
            :value="scope.row.bayonetRdCode"
          ></dict-tag>
        </template>

        <template #declTrafMode="scope">
          <dict-tag
            :options="dict.type.sgs_decl_traf_mode"
            :value="scope.row.declTrafMode"
          ></dict-tag>
        </template>
        <template #customsCode="scope">
          <dict-value
            :list="customsList"
            :dictCode="scope.row.customsCode"
          ></dict-value>
        </template>
        <template #customsMaster="scope">
          <dict-value
            :list="customsList"
            :dictCode="scope.row.customsMaster"
          ></dict-value>
        </template>

        <template #declStatus="scope">
          <dict-tag
            :options="dict.type.sgs_decl_status"
            :value="scope.row.declStatus"
          ></dict-tag>
        </template>
        <template #rcptStatus="scope">
          <dict-tag
            :options="dict.type.sgs_rcpt_status"
            :value="scope.row.rcptStatus"
          ></dict-tag>
        </template>

        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('edit', scope.row)"
              v-hasPermi="['bt:sgsBulkCargoHead:edit']"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDelete(scope.row)"
              v-hasPermi="['bt:sgsBulkCargoHead:remove']"
            >
              删除
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit
      ref="addEdit"
      :dialogType="dialogType"
      :rowData="rowData"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      :dict="dictProp"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <detail-dialog
      ref="detail"
      :detailId="detailId"
      :customsList="customsList"
      :dict="dictProp"
    ></detail-dialog>
  </div>
</template>

<script>
import {
  listSgsBulkCargoHead,
  delSgsBulkCargoHead,
  exportSgsBulkCargoHead,
  declBulkCargoRecord,
  canceldeclBulkCargoRecord,
  genTakeCargoBill
} from "@/api/swdecl/sgsBulkCargoHead";
import addEdit from "./components/addEdit";
import detailDialog from "./components/detailDialog";
import {findListCustoms} from "@/api/system/customs";
export default {
  name: "SgsBulkCargoHead",
  components: {
    addEdit,
    detailDialog,
  },
  dicts: [
    "sgs_bind_key_type",
    "sgs_bill_rd_code",
    "sys_i_e_flag",
    "sgs_bayonet_rd_code",
    "sgs_decl_status",
    "sgs_decl_traf_mode",
    "sgs_rcpt_status"
  ],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rdCode: null,
        iEFlag: null,
        customsCode: null,
        fieldCode: null,
        bayonetRdCode: null,
        declStatus: null,
        entCode: null,
        createTime: null,
      },
      // 遮罩层
      loading: true,
      // 列表数据
      tableData: [],
      columns: [
        {
          prop: "transportName",
          label: "车牌号",
          align: "center",
          minWidth: 120
        },
        {
          prop: "bindKeyType",
          label: "绑定介质类型",
          align: "center",
          slot: true,
          minWidth: 120
        },
        {
          prop: "bindKeyInfo",
          label: "绑定介质关键信息",
          align: "center",
          minWidth: 130
        },
        {
          prop: "sureId",
          label: "车次确认号",
          align: "center",
           minWidth: 130
         },
        {
          prop: "rdCode",
          label: "业务类型",
          align: "center",
          slot: true,
          minWidth: 100
        },
        {
          prop: "bayonetRdCode",
          label: "过卡车辆类型",
          align: "center",
          slot: true,
          minWidth: 110
        },
        {
          prop: "iEFlag",
          label: "进出口标志",
          align: "center",
          slot: true,
          minWidth: 100
        },
        {
          prop: "customsCode",
          label: "关区",
          align: "center",
          minWidth: 120,
          slot: true
        },
        {
          prop: "customsMaster",
          label: "过卡海关",
          align: "center",
          minWidth: 120,
          slot: true
        },
        {
          prop: "bayonetFieldName",
          label: "过卡场所名称",
          align: "center",
          minWidth: 120
        },

        {
          prop: "vehicleWeight",
          label: "车辆重量",
          align: "center",
        },

        {
          prop: "trailerWeight",
          label: "挂车重量",
          align: "center",
        },

        {
          prop: "contaWeight",
          label: "集装箱重量",
          align: "center",
          minWidth: 110
        },
 {
          prop: "createTime",
          label: "创建时间",
          align: "center",
        },

        {
          prop: "declStatus",
          label: "申报状态",
          align: "center",
          slot: true,
          fixed: "right",
          minWidth: 120
        },
        {
          prop: "declTime",
          label: "申报时间",
          align: "center",
        },
        {
          prop: "rcptStatus",
          label: "回执状态",
          align: "center",
          slot: true,
          fixed: "right",
          minWidth: 120
        },
        {
          prop: "rcptRemark",
          label: "回执说明",
          align: "center",
        },


        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 140,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      dialogType: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      selectRow: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,

      // 散杂货申报管理表格数据
      sgsBulkCargoHeadList: [],
      // 报关企业代码时间范围
      daterangeCreateTime: [],
      //海关关区信息列表
      customsList: [],
      // 场所列表
      fieldOptions: [],
    };
  },
  computed: {
    dictProp() {
      //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type,
      };
    },
  },
  created() {
    this.getList();
    this.loadCustomsList();
  },
  methods: {
    /** 查询散杂货申报管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listSgsBulkCargoHead(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      findListCustoms().then((response) => {
        this.customsList = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.pageParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      if(this.ids.length === 1) {
        this.rowData = JSON.parse(JSON.stringify(this.selectRow));
      }
      this.$refs.addEdit.open();
    },
    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      if(selection.length === 1) {
        this.selectRow = selection[0];
      } else {
        this.selectRow = {};
      }
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除散杂货申报管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSgsBulkCargoHead(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //申报
    handleDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要申报的散杂货单证！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declBulkCargoRecord(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    //生成作业单
    handleGenTakecargoBill(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要申报的散杂货单证！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return genTakeCargoBill(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },


    //撤销申报
    handleCancelDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要撤销申报的散杂货单证！");
        return false;
      }
      this.$modal.confirm('是否确认撤销申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return canceldeclBulkCargoRecord(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销申报成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有散杂货申报管理数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportSgsBulkCargoHead(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>



<style lang="scss" scoped>
.basic-form {
  ::v-deep .el-form-item__label {
    line-height: 16px;
    display: flex;
    align-items: center;
    height: 32px;
    justify-content: flex-end;
  }
}
</style>
